import java.util.Random;

/**
 * 加减法口算练习系统 v0.2
 * 支持100以上的数和负数，包含答案验证
 */
public class CalculationSystemV02 {

    private static Random random = new Random();

    /**
     * 生成指定范围内的随机数
     * @param min 最小值
     * @param max 最大值
     * @return 范围内的随机整数
     */
    public static int generateRandomNumber(int min, int max) {
        return random.nextInt(max - min + 1) + min;
    }

    /**
     * 生成有效的加法算式（和不超过100）
     * @return 包含答案的算式字符串
     */
    public static String generateAdditionEquation() {
        int num1, num2, sum;

        do {
            num1 = generateRandomNumber(0, 100);
            num2 = generateRandomNumber(0, 100);
            sum = num1 + num2;
        } while (sum > 100); // 确保和不超过100

        return String.format("%2d + %2d = %3d", num1, num2, sum);
    }

    /**
     * 生成有效的减法算式（差不小于0）
     * @return 包含答案的算式字符串
     */
    public static String generateSubtractionEquation() {
        int num1, num2, difference;

        do {
            num1 = generateRandomNumber(0, 100);
            num2 = generateRandomNumber(0, 100);
            difference = num1 - num2;
        } while (difference < 0); // 确保差不小于0

        return String.format("%2d - %2d = %3d", num1, num2, difference);
    }

    /**
     * 生成单个算式（随机选择加法或减法）
     * @return 包含答案的算式字符串
     */
    public static String generateEquation() {
        boolean isAddition = random.nextBoolean();
        if (isAddition) {
            return generateAdditionEquation();
        } else {
            return generateSubtractionEquation();
        }
    }

    /**
     * 生成指定数量的算式
     * @param count 算式数量
     * @return 算式数组
     */
    public static String[] generateEquations(int count) {
        String[] equations = new String[count];
        for (int i = 0; i < count; i++) {
            equations[i] = generateEquation();
        }
        return equations;
    }

    /**
     * 格式化输出算式，每行显示多个
     * @param equations 算式数组
     * @param perLine 每行显示的算式数量
     */
    public static void printEquations(String[] equations, int perLine) {
        System.out.println("序号\t算式");
        System.out.println("----\t----------------");

        for (int i = 0; i < equations.length; i++) {
            System.out.printf("%2d.\t%s\t", i + 1, equations[i]);
            if ((i + 1) % perLine == 0) {
                System.out.println();
            }
        }
        // 如果最后一行不满，换行
        if (equations.length % perLine != 0) {
            System.out.println();
        }
    }

    public static void main(String[] args) {
        System.out.println("加减法口算练习系统 v0.2");
        System.out.println("======================");

        // 生成50道算式1
        String[] equations = generateEquations(50);

        // 每行显示3个算式
        printEquations(equations, 3);

        System.out.println("======================");
        System.out.println("共生成 " + equations.length + " 道算式");

        // 统计信息
        int additionCount = 0;
        int subtractionCount = 0;
        for (String eq : equations) {
            if (eq.contains("+")) {
                additionCount++;
            } else {
                subtractionCount++;
            }
        }
        System.out.println("其中加法: " + additionCount + " 道，减法: " + subtractionCount + " 道");
    }
}